package com.aiatomic.couting.widgets;

import java.text.SimpleDateFormat;
import java.util.List;

import com.aiatomic.couting.AccountInfoActivity;
import com.aiatomic.couting.PayActivity;
import com.aiatomic.couting.R;
import com.aiatomic.couting.models.OrderEntity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class OrderAdapter extends ArrayAdapter<OrderEntity> {
	SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private AccountInfoActivity activity;

	public OrderAdapter(Context context, int resource, List<OrderEntity> objects) {
		super(context, resource, objects);
		activity=(AccountInfoActivity) context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final OrderEntity dataItem = this.getItem(position);
		view = LayoutInflater.from(getContext()).inflate(R.layout.custom_order_list_item, null);
		((TextView) view.findViewById(R.id.product_name)).setText(dataItem.getGood_name());
		Button buyAgain=((Button) view.findViewById(R.id.product_buy_again));
		Button pay=((Button) view.findViewById(R.id.product_pay));

		
		buyAgain.setVisibility(View.INVISIBLE);
		switch (dataItem.getState()) {
		case PAY_FIN:
			((TextView) view.findViewById(R.id.product_status)).setText(R.string.PAY_FIN);
			buyAgain.setVisibility(View.VISIBLE);
			pay.setVisibility(View.INVISIBLE);
			break;
		case PAY_CLOSE:
			((TextView) view.findViewById(R.id.product_status)).setText(R.string.PAY_CLOSE);
			pay.setVisibility(View.INVISIBLE);

			break;
		case PAY_SUCCESS:
			buyAgain.setVisibility(View.VISIBLE);
			pay.setVisibility(View.INVISIBLE);
			((TextView) view.findViewById(R.id.product_status)).setText(R.string.PAY_SUCCESS);
			break;
		case PAY_WAIT:
			((TextView) view.findViewById(R.id.product_status)).setText(R.string.PAY_WAIT);
			pay.setVisibility(View.VISIBLE);
			break;
		case PAY_ADD:
			pay.setVisibility(View.VISIBLE);
			((TextView) view.findViewById(R.id.product_status)).setText(R.string.PAY_ADD);
			break;
		
		}
		((TextView) view.findViewById(R.id.product_real_give_day))
				.setText(String.valueOf(dataItem.getReal_give_days()));
		((TextView) view.findViewById(R.id.product_real_pay)).setText(String.valueOf(dataItem.getReal_money()));
		((TextView) view.findViewById(R.id.product_date)).setText(dateformat.format(dataItem.getCreated_time_loc()));
		view.findViewById(R.id.product_buy_again).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.addOrder(dataItem.getGood_id());
			}
		});
		pay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, PayActivity.class);
				intent.putExtra("oid", dataItem.getGm_order_id());
				intent.putExtra("real_price", String.valueOf(dataItem.getReal_money()));
				intent.putExtra("name", dataItem.getGood_name());
				intent.putExtra("desc", dataItem.getGood_desc());
				intent.putExtra("order", dataItem);
				activity.startActivity(intent);
			}
		});
		return view;
	}
}
